package infoManage.controller;

import infoManage.domain.Teacher;
import infoManage.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private static Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    editTeacher();
                    break;
                case "4":
                    queryTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统, 再见!");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误，请重新输入：");
            }
        }
    }

    private void editTeacher() { // 修改
        String id = inputTeacherId();
        Teacher tec = inputTeacherInfo(id);
        teacherService.editTeacher(tec);
        System.out.println("修改成功");
    }

    private void queryTeacher() { // 查看
        Teacher[] teachers = teacherService.findAllTeacher();
        if(teachers == null) {
            System.out.println("查无信息, 请添加后重试");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher tec = teachers[i];
            if(tec != null) System.out.println(tec.getId() + "\t" + tec.getName() + "\t" + tec.getAge() + "\t" + tec.getBirthday());
        }
    }

    private void deleteTeacher() { // 删除
        String id = inputTeacherId();
        teacherService.deleteTeacher(id);
        System.out.println("删除成功");
    }

    private void addTeacher() { // 添加
        String id;
        while (true) {
            System.out.println("请输入老师 id:");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("id被占用，请重新输入:");
            } else {
                break;
            }
        }
        Teacher teacher = inputTeacherInfo(id);
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Teacher inputTeacherInfo(String id) { // 录入老师信息
        System.out.println("请输入老师姓名:");
        String name = sc.next();
        System.out.println("请输入老师年龄:");
        String age = sc.next();
        System.out.println("请输入老师生日:");
        String bir = sc.next();
        Teacher teacher = new Teacher(id, name, age, bir);
        return teacher;
    }

    private String inputTeacherId() { // 输入老师 id
        String id;
        while(true) {
            System.out.println("请输入老师的 id:");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists) {
                break;
            } else {
                System.out.println("您输入的id不存在, 请重新输入:");
            }
        }
        return id;
    }
}
